package logic

import (
	"context"
	"fmt"
	"goods/crud"

	"goods/goods"
	"goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGoodLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGoodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGoodLogic {
	return &GetGoodLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetGoodLogic) GetGood(in *goods.GetGoodRequest) (*goods.GetGoodResponse, error) {
	goodMod := crud.NewGoodsModel(crud.Client)
	goodInfo, err := goodMod.FindOne(l.ctx, in.ID)
	if err != nil {
		return nil, err
	}

	return &goods.GetGoodResponse{
		Info: &goods.Good{
			ID:      fmt.Sprintf("%v", goodInfo.Id),
			Name:    goodInfo.Name,
			Content: goodInfo.Content,
			Price:   goodInfo.Price,
		},
	}, nil
}
